package qldc.pm.ksdl.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import qldc.pm.qtht.dmdc.model.NganhNgheCoDieuKien;
import qldc.pm.qtht.dmdc.model.PhuongXa;
import qldc.pm.qtht.dmdc.model.QuanHuyen;
import qldc.pm.qtht.dmdc.model.TrangThaiHoSo;
import qldc.pm.qtht.dmdc.service.NganhNgheCoDieuKienLocalServiceUtil;
import qldc.pm.qtht.dmdc.service.PhuongXaLocalServiceUtil;
import qldc.pm.qtht.dmdc.service.QuanHuyenLocalServiceUtil;
import qldc.pm.qtht.dmdc.service.TrangThaiHoSoLocalServiceUtil;
import qldc.pm.qtht.utils.NguoiDungUtils;
import qldc.pm.service.utils.maphatsinh.MaPhatSinhFactory;
import qldc.pm.service.utils.maphatsinh.TenMaPhatSinh;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;

/**
 * The Class ResourceActionHandler.
 */
public class ResourceActionHandler {
	
	/** The Constant LOG. */
	private static final Log log = LogFactoryUtil.getLog(ResourceActionHandler.class);
	
	// Load Quan Huyen based on Tinh
	/**
	 * Load quan huyen.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void loadQuanHuyen (ResourceRequest request, ResourceResponse response) throws IOException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long tinhId = ParamUtil.getLong(request, "tinhId_", 0);
		if (tinhId > 0) {
			List<QuanHuyen> quanHuyenList = QuanHuyenLocalServiceUtil.findByTinhId(tinhId);
			for (QuanHuyen quanHuyen : quanHuyenList) {
				JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
				jsonObject.put("quanHuyenId", quanHuyen.getQuanHuyenId());
				jsonObject.put("tenQuanHuyen", quanHuyen.getTenQuanHuyen());

				jsonArray.put(jsonObject);
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}

	// Load Phuong Xa based on Quan/Huyen
	/**
	 * Load phuong xa.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void loadPhuongXa (ResourceRequest request, ResourceResponse response) throws IOException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long quanHuyenId = ParamUtil.getLong(request, "quanHuyenId_", 0);
		if (quanHuyenId > 0) {
			List<PhuongXa> phuongXaList = PhuongXaLocalServiceUtil.findByQuanHuyenId(quanHuyenId);
			for (PhuongXa item : phuongXaList) {
				JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
				jsonObject.put("phuongXaId", item.getPhuongXaId());
				jsonObject.put("tenPhuongXa", item.getTenPhuongXa());

				jsonArray.put(jsonObject);
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}

	/**
	 * Load nganh nghe co dk.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void loadNganhNgheCoDK (ResourceRequest request, ResourceResponse response) throws IOException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long nhomNganhNgheId = ParamUtil.getLong(request, "nhomNganhNgheId_", 0);
		if (nhomNganhNgheId > 0) {
			List<NganhNgheCoDieuKien> nganhNgheList = NganhNgheCoDieuKienLocalServiceUtil.findByNhomNganhNgheCoDieuKienId(nhomNganhNgheId);
			for (NganhNgheCoDieuKien item : nganhNgheList) {
				JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
				jsonObject.put("nganhNgheId", item.getNganhNgheCoDieuKienId());
				jsonObject.put("tenNganhNghe", item.getTenNganhNgheCoDieuKien());

				jsonArray.put(jsonObject);
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}
	
	/**
	 * Load trang thai ho so.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void loadTrangThaiHoSo (ResourceRequest request, ResourceResponse response) throws IOException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long loaiHoSoId = ParamUtil.getLong(request, "loaiHoSoId_", 0);
		if (loaiHoSoId > 0) {
			List<TrangThaiHoSo> trangThaiList = TrangThaiHoSoLocalServiceUtil.findByLoaiHoSoId(loaiHoSoId);
			for (TrangThaiHoSo item : trangThaiList) {
				JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
				jsonObject.put("trangThaiHoSoId", item.getTrangThaiHoSoId());
				jsonObject.put("tenTrangThai", item.getTenTrangThaiHoSo());

				jsonArray.put(jsonObject);
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}

	/**
	 * Lay so bien nhan.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws SystemException the system exception
	 */
	public void laySoBienNhan(ResourceRequest request, ResourceResponse response) throws IOException, SystemException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long userId = PortalUtil.getUserId(request);

		String soBienNhan = "";

		if(userId > 0) {
			Map<String, String> diaChiNguoiDungs = NguoiDungUtils.getDiaChiNguoiDung(userId);

			String maTinh = "";

			String maQuanHuyen = "";

			String maPhuongXa = "";

			if(Validator.isNotNull(diaChiNguoiDungs) && !diaChiNguoiDungs.isEmpty()) {
				maTinh = diaChiNguoiDungs.get(NguoiDungUtils.MA_TINH);

				maQuanHuyen = diaChiNguoiDungs.get(NguoiDungUtils.MA_QUAN_HUYEN);

				maPhuongXa = diaChiNguoiDungs.get(NguoiDungUtils.MA_PHUONG_XA);
			}
			soBienNhan = MaPhatSinhFactory.getMaPhatSinh(TenMaPhatSinh.BIEN_NHAN_QLNNCDK, maTinh, maQuanHuyen, maPhuongXa);
		}

		JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

		jsonObject.put("soBienNhan", soBienNhan);

		jsonArray.put(jsonObject);

		pw.print(jsonArray.toString());

		pw.flush();
	}

	/**
	 * Lay so ho so.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws SystemException the system exception
	 */
	public void laySoHoSo(ResourceRequest request, ResourceResponse response) throws IOException, SystemException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long userId = PortalUtil.getUserId(request);

		String soHoSo = "";

		if(userId > 0) {
			Map<String, String> diaChiNguoiDungs = NguoiDungUtils.getDiaChiNguoiDung(userId);

			String maTinh = "";

			String maQuanHuyen = "";

			String maPhuongXa = "";

			if(Validator.isNotNull(diaChiNguoiDungs) && !diaChiNguoiDungs.isEmpty()) {
				maTinh = diaChiNguoiDungs.get(NguoiDungUtils.MA_TINH);

				maQuanHuyen = diaChiNguoiDungs.get(NguoiDungUtils.MA_QUAN_HUYEN);

				maPhuongXa = diaChiNguoiDungs.get(NguoiDungUtils.MA_PHUONG_XA);
			}

			soHoSo = MaPhatSinhFactory.getMaPhatSinh(TenMaPhatSinh.HO_SO_QLNNCDK, maTinh, maQuanHuyen, maPhuongXa);
		}

		JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

		jsonObject.put("soHoSo", soHoSo);

		jsonArray.put(jsonObject);

		pw.print(jsonArray.toString());

		pw.flush();
	}
	
}
